package com.iotmonitor.iot.controller;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.iotmonitor.common.core.web.controller.BaseController;
import com.iotmonitor.common.core.web.domain.AjaxResult;
import com.iotmonitor.common.core.web.page.PageDomain;
import com.iotmonitor.common.core.web.page.TableDataInfo;
import com.iotmonitor.common.core.web.page.TableSupport;
import com.iotmonitor.iot.domain.dto.DeviceDTO;
import com.iotmonitor.iot.domain.vo.DeviceQuotaVO;
import com.iotmonitor.iot.domain.vo.DeviceVO;
import com.iotmonitor.iot.service.TbDeviceService;
import com.iotmonitor.iot.service.TbNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/device")
public class TbDeviceController extends BaseController {

    @Autowired
    private TbDeviceService tbDeviceService;

    @Autowired
    private TbNoticeService noticeService;


    /**
     * 设置设备状态
     *
     * @param deviceVO
     * @return
     */
    @PutMapping("/status")
    public boolean setStatus(@RequestBody DeviceVO deviceVO) {

        return tbDeviceService.setStatus(deviceVO.getSn(), deviceVO.getStatus());

    }


    /**
     * 设置设备标签
     *
     * @param deviceVO
     * @return
     */
    @PutMapping("/tags")
    public boolean setTags(@RequestBody DeviceVO deviceVO) {

        return tbDeviceService.updateTags(deviceVO.getSn(), deviceVO.getTags());

    }

    /**
     * 添加设备标签
     *
     * @param deviceVO
     * @return
     */
    @PostMapping("/tags/add")
    public AjaxResult addTag(@RequestBody DeviceVO deviceVO) {
        return AjaxResult.success(tbDeviceService.addTag(deviceVO.getSn(), deviceVO.getTags()));
    }

    /**
     * 删除设备标签
     *
     * @param
     * @return
     */
    @DeleteMapping("/tags/{deviceId}")
    public AjaxResult deleteTag(@PathVariable String deviceId, @RequestParam String tag) {
        return AjaxResult.success(tbDeviceService.deleteTag(deviceId, tag));
    }


    /**
     * 分页查询设备
     * @param sn
     * @param tag
     * @return
     */
    @GetMapping
    public TableDataInfo findPage(@RequestParam(value = "sn", required = false) String sn,
                                     @RequestParam(value = "tag", required = false) String tag) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        List<DeviceDTO> deviceDTOPager = tbDeviceService.queryPage(pageNum, pageSize, sn, tag, null);
        return getDataTable(deviceDTOPager);
    }

    /**
     * 接收设备断连信息
     *
     * @param jsonParam
     */
    @PostMapping("/clientAction")
    public void clientAction(@RequestBody JSONObject jsonParam) {
        Map<String, Object> result = JSON.parseObject(String.valueOf(jsonParam), Map.class);
        logger.info(result.toString());
        // 提取设备id
        String deviceId = result.get("clientid").toString();
        if (result.get("event").equals("client.connected")) {
            // 如果是联网
            tbDeviceService.updateOnLine(deviceId, true);
            // 联网通知
            noticeService.onlineTransfer(deviceId, true);
        }
        if (result.get("event").equals("client.disconnected")) {
            // 如果是断网
            tbDeviceService.updateOnLine(deviceId, false);
            // 断网通知
            noticeService.onlineTransfer(deviceId, false);
        }
    }


    /**
     * 设备详情
     *
     * @param deviceId
     * @param tag
     * @param state
     * @return
     */
    @GetMapping("/deviceQuota")
    public TableDataInfo queryQuotaData(@RequestParam(value = "deviceId", required = false) String deviceId,
                                        @RequestParam(value = "tag", required = false) String tag,
                                        @RequestParam(value = "state", required = false) Integer state) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        List<DeviceQuotaVO> deviceQuotaVOS = tbDeviceService.queryDeviceQuota(pageNum, pageSize, deviceId, tag, state);
        return getDataTable(deviceQuotaVOS);
    }


}
